package com.yantu.controller.student;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yantu.bean.ExamPaper;
import com.yantu.bean.Subject;
import com.yantu.mapper.ExamPaperMapper;
import com.yantu.mapper.SubjectMapper;
import com.yantu.util.Result;
import com.yantu.view.student.StudentPaperView;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@ApiModel("学生试卷接口信息")
@RequestMapping("/student")
public class StudentExamPaperController {


    @Autowired
    ExamPaperMapper examPaperMapper;
    @Autowired
    SubjectMapper subjectMapper;


    //试卷分页
    @GetMapping("/papers")
    @ApiOperation("试卷分页信息")
    public Result studentPaper(@ModelAttribute() StudentPaperView studentPaperView) {
        PageHelper.startPage(studentPaperView.getPageIndex(), studentPaperView.getPageSize());

        QueryWrapper<ExamPaper> queryWrapper = new QueryWrapper<>();
        if(studentPaperView.getPaperType()!=null){
        queryWrapper.eq("paper_type", studentPaperView.getPaperType());}
        if(studentPaperView.getSubjectId()!=null){
        queryWrapper.eq("subject_id", studentPaperView.getSubjectId());}




        List<ExamPaper> examPapers = examPaperMapper.selectList(queryWrapper);
        if(examPapers!=null) {
            PageInfo pageInfo = new PageInfo(examPapers);
            List<ExamPaper> examPapers1 = pageInfo.getList();
            Map<String, Object> map = new HashMap<>();
            map.put("total", pageInfo.getTotal());
            List<Object> list1 = new ArrayList();
            for (ExamPaper paper : examPapers1) {
                Map<String, Object> map1 = new HashMap<>();
                map1.put("id", paper.getId());
                map1.put("name", paper.getName());
                map1.put("questionCount", paper.getQuestionCount());
                map1.put("score", paper.getScore());
                map1.put("createTime", paper.getCreateTime());
                map1.put("createUser", paper.getCreateUser());
                map1.put("subjectId", paper.getSubjectId());
                QueryWrapper<Subject> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("id", paper.getSubjectId());
                Subject subject = subjectMapper.selectOne(queryWrapper1);
                map1.put("subjectName", subject.getName());
                map1.put("paperType", paper.getPaperType());
                map1.put("frameTextContentId", paper.getFrameTextContentId());
                list1.add(map1);
            }
            map.put("list", list1);
            return Result.succ(200, "查询成功", map);
        }

        else return Result.succ(200, "查询不到数据", null);
    }
}
